//
//  ZTimeOutOrderChildVC.m
//  DistributorAB
//
//  Created by LZY on 2019/10/16.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "ZTimeOutOrderChildVC.h"
#import "ZTimeOutOrderCell.h"
#import "ZTimeOutOrderUntreatedVC.h"//未处理
#import "ZTimeOutOrderTreatingVC.h"//处理中
#import "ZTimeOutOrderTreatedVC.h"//已处理
#import "ZTimeOutOrderModel.h"

@interface ZTimeOutOrderChildVC () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *orderArray;

@end

@implementation ZTimeOutOrderChildVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = kColorWithHex(0xf2f2f2);
    _orderArray = [NSMutableArray array];
    
    [self setupViewAction];
    
    [self requestData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:USER_DEAL_TIME_OUT_ORDER_SUCCESS object:nil];
}

#pragma mark - 界面布局
- (void)setupViewAction{
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.backgroundColor = kColorWithHex(0xf2f2f2);
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    //空态界面展示
    KKTableViewNoDataView *viewPlaceHolder = [[KKTableViewNoDataView alloc] initWithFrame:CGRectMake((kScreen_Width - 100) / 2.0, (kScreen_Height - 72) / 2.0, 100, 72) image:imgname(@"tableviewPlaseholder") viewClick:^{
    }];
    viewPlaceHolder.backgroundColor = kColorWithHex(0xf2f2f2);
    _tableView.placeHolderView = viewPlaceHolder;
    
    [_tableView registerClass:[ZTimeOutOrderCell class] forCellReuseIdentifier:NSStringFromClass([ZTimeOutOrderCell class])];
    
}
#pragma mark - 数据请求
- (void)requestData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (self.type == ZTimeOutOrderChildVCTypeAll) {
        [param setObjectIfNotNil:@"" forKey:@"state"];
    }else if (self.type == ZTimeOutOrderChildVCTypeUntreated){
        [param setObjectIfNotNil:@"0" forKey:@"state"];
    }else if (self.type == ZTimeOutOrderChildVCTypeTreating){
        [param setObjectIfNotNil:@"1" forKey:@"state"];
    }else if (self.type == ZTimeOutOrderChildVCTypeTreated){
        [param setObjectIfNotNil:@"2" forKey:@"state"];
    }
    kWeakSelf;
    [NetWorkTool POST:USER_OVERTIME_APP_ORDERS param:param success:^(id dic) {
        NSArray *orderList = [dic objectForKeyNotNil:@"data"];
        [weakSelf.orderArray removeAllObjects];
        [orderList enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            ZTimeOutOrderModel *model = [ZTimeOutOrderModel mj_objectWithKeyValues:obj];
            [weakSelf.orderArray addObjectNotNil:model];
        }];
        [weakSelf.tableView reloadData];
    } other:^(id dic) {
        
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.orderArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZTimeOutOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZTimeOutOrderCell class]) forIndexPath:indexPath];
    ZTimeOutOrderModel *model = [self.orderArray objectAtIndexSafe:indexPath.row];
    cell.model = model;
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [ZTimeOutOrderCell cellHeight];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZTimeOutOrderModel *model = [self.orderArray objectAtIndexSafe:indexPath.row];
    if (AGENT_LEVEL == 1) {
        //根据model.delstate在cell创建不同展示内容
        ZTimeOutOrderUntreatedVC *vc = [ZTimeOutOrderUntreatedVC new];
        vc.orderModel = model;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        NSInteger delState = [model.delstate integerValue];
        if (delState == 0) {
            //未处理
            ZTimeOutOrderUntreatedVC *vc = [ZTimeOutOrderUntreatedVC new];
            vc.orderModel = model;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (delState == 1){
            //处理中
            ZTimeOutOrderTreatingVC *vc = [ZTimeOutOrderTreatingVC new];
            vc.orderID = model.Id;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (delState == 2){
            //已处理
            ZTimeOutOrderTreatedVC *vc = [ZTimeOutOrderTreatedVC new];
            vc.orderModel = model;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
